package com.gdface.test.permissiontest;

import android.Manifest;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.gdface.test.permissiontest.utils.PermissionUtils;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private final String PERMISSION_CAMERA = Manifest.permission.CAMERA;
    private final String[] PERMISSIONS = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.READ_CALENDAR, Manifest.permission.ACCESS_COARSE_LOCATION};

    private final int REQUEST_CODE_CAMERA = 1, REQUEST_CODE_PERMISSIONS = 2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_permission:
                requestPermission();
                break;
            case R.id.btn_permissions:
                requestPermissions();
                break;
            case R.id.btn_permission_zdy:
                requestPermissionCustom();
                break;
            case R.id.btn_permissions_zdy:
                requestPermissionsCusom();
                break;
        }
    }

    //普通申请一个权限
    private void requestPermission() {
        PermissionUtils.checkAndRequestPermission(this, PERMISSION_CAMERA, REQUEST_CODE_CAMERA,
                new PermissionUtils.PermissionRequestSuccessCallBack() {
                    @Override
                    public void onHasPermission() {
                        Log.i("hwwlog", "普通申请一个权限     onHasPermission");
                    }
                });
    }

    //普通申请多个权限
    private void requestPermissions() {
        PermissionUtils.checkAndRequestMorePermissions(this, PERMISSIONS, REQUEST_CODE_PERMISSIONS,
                new PermissionUtils.PermissionRequestSuccessCallBack() {
                    @Override
                    public void onHasPermission() {
                        Log.i("hwwlog", "普通申请多个权限     onHasPermission");
                    }
                });
    }

    //自定义申请一个权限
    private void requestPermissionCustom() {
        PermissionUtils.checkPermission(this, PERMISSION_CAMERA,
                new PermissionUtils.PermissionCheckCallBack() {
                    @Override
                    public void onHasPermission() {
                        Log.i("hwwlog", "自定义申请一个权限     onHasPermission");
                    }

                    @Override
                    public void onUserHasAlreadyTurnedDown(String... permission) {
                        showExplainDialog(permission, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {

                                PermissionUtils.requestPermission(MainActivity.this, PERMISSION_CAMERA, REQUEST_CODE_CAMERA);
                            }
                        });
                    }

                    @Override
                    public void onUserHasAlreadyTurnedDownAndDontAsk(String... permission) {
                        PermissionUtils.requestPermission(MainActivity.this, PERMISSION_CAMERA, REQUEST_CODE_CAMERA);
                    }
                });
    }

    //自定义申请多个权限
    private void requestPermissionsCusom() {

        PermissionUtils.checkMorePermissions(MainActivity.this, PERMISSIONS,
                new PermissionUtils.PermissionCheckCallBack() {
                    @Override
                    public void onHasPermission() {
                        Log.i("hwwlog", "自定义申请多个权限     onHasPermission");
                    }

                    @Override
                    public void onUserHasAlreadyTurnedDown(String... permission) {
                        showExplainDialog(permission, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                PermissionUtils.requestMorePermissions(MainActivity.this, PERMISSIONS, REQUEST_CODE_PERMISSIONS);
                            }
                        });
                    }

                    @Override
                    public void onUserHasAlreadyTurnedDownAndDontAsk(String... permission) {
                        PermissionUtils.requestMorePermissions(MainActivity.this, PERMISSIONS, REQUEST_CODE_PERMISSIONS);
                    }
                });

    }


    private void toCamera() {
        Intent intent = new Intent();
        intent.setAction("android.media.action.STILL_IMAGE_CAMERA");
        startActivity(intent);
    }


    //解释权限的Dialog
    private void showExplainDialog(String[] permission, DialogInterface.OnClickListener onClickListener) {
        new AlertDialog.Builder(this)
                .setTitle("申请权限")
                .setMessage("我们需要" + Arrays.toString(permission) + "权限")
                .setPositiveButton("确定", onClickListener)
                .show();
    }

    //显示前往应用设置Dialog
    private void showToAppSettingDialog() {
        new AlertDialog.Builder(this)
                .setTitle("需要权限")
                .setMessage("我们需要相关权限，才能实现功能，点击前往，将转到应用的设置界面，请开启应用的相关权限。")
                .setPositiveButton("前往", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        PermissionUtils.toAppSetting(MainActivity.this);
                    }
                })
                .setNegativeButton("取消", null)
                .show();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_CODE_CAMERA:
                if (PermissionUtils.isPermissionRequestSuccess(grantResults)) {
                    Log.i("hwwlog", "REQUEST_CODE_CAMERA     success");
                } else {
                    Toast.makeText(this, "打开相机失败", Toast.LENGTH_SHORT).show();
                }
                break;
            case REQUEST_CODE_PERMISSIONS:
                PermissionUtils.onRequestMorePermissionsResult(this, PERMISSIONS, new PermissionUtils.PermissionCheckCallBack() {
                    @Override
                    public void onHasPermission() {
                        Log.i("hwwlog", "REQUEST_CODE_PERMISSIONS    onHasPermission    success");
                    }

                    @Override
                    public void onUserHasAlreadyTurnedDown(String... permission) {
                        Toast.makeText(MainActivity.this, "" + Arrays.toString(permission), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onUserHasAlreadyTurnedDownAndDontAsk(String... permission) {
                        Toast.makeText(MainActivity.this, "我们需要" + Arrays.toString(permission) + "权限", Toast.LENGTH_SHORT).show();
                        showToAppSettingDialog();
                    }
                });
                break;
        }
    }
}
